$(document).ready(function() {
    $('input[type="text"]').focus(function() {
        this.select();
    }).blur(function() {
        if (jQuery.trim(this.value) == '') {
            this.value = (this.defaultValue ? this.defaultValue : '');

        }
    });
    var act_search = false;
    $("#btn_search_customer").click(function() {
        if (act_search === false) {
            act_search = true;

            $.post("ajax/searchCustomer.php", {
                text_search: $('#filter_search_customer').val(),
                page: 1
            }, function(res) {
                if (res.type == 0) {
                    $("#status_search").val(1);
                    $('#tbody_customer_list').html(res.html);
                    $('#page_customer').val(res.page);
                    $('#loadmore_customer').val(res.load_more);
                    delete_customer();

                } else {
                    alert(res.msg);
                }
                act_search = false;
            }, "json");

        } else {
            return false;
        }
    });
    $("#reset_search").click(function() {
        $("#status_search").val(0);
        window.location.reload();
    });
    delete_customer();
//paging
    var killScroll = false;
    $(window).scroll(function() {
        if ($('#loadmore_customer').val() == 1) {
            if ($(window).scrollTop() > ($(document).height() - $(window).height() - 100)) {
                $("#loading").show();
                if (killScroll === false) {
                    killScroll = true;
                    if ($("#status_search").val() == 0) {
                        $.post("ajax/loadMoreCustomer.php", {
                            page: $('#page_customer').val()

                        }, function(res) {
                            $("#loading").hide();
                            $("#tbody_customer_list").append(res.html);
                            $('#page_customer').val(res.page);
                            $('#loadmore_customer').val(res.load_more);
                            delete_customer();
                            killScroll = false;

                        }, "json");
                    } else {
                        $.post("ajax/searchCustomer.php", {
                            text_search: $('#filter_search_customer').val(),
                            page: $('#page_customer').val()
                        }, function(res) {
                            if (res.type == 0) {
                                $("#status_search").val(1);
                                $('#tbody_customer_list').html(res.html);
                                $('#page_customer').val(res.page);
                                $('#loadmore_customer').val(res.load_more);
                                delete_customer();

                            } else {
                                alert(res.msg);
                            }
                            act_search = false;
                        }, "json");
                    }
                } else {
                    return false;
                }
            }
        }
    });
});
function delete_customer() {
    var act_del = false;
    $(".btn_delete_customer").unbind('click');
    $(".btn_delete_customer").click(function() {
        var btn_del = $(this);
        if (confirm("Are you sure you want to delete this customer?")) {
            if (act_del === false) {
                act_del = true;
                $.post("ajax/deleteCustomer.php", {
                    customer_id: $(this).attr('rel')
                }, function(res) {
                    act_del = false;
                    alert(res.msg);
                    if (res.type == 0) {
                        btn_del.parent().parent().remove();
                    }

                }, "json");


            } else {
                return false;
            }
        } else {
            return false;
        }
    });
}


